package com.waimai.mobile.ui;

import android.app.Application;
import android.widget.Toast;

import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.User;
import com.waimai.mobile.api.model.ResponseModel;
import com.waimai.mobile.misc.AppPreferences_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EApplication;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

/**
 * Created by lin on 2015/6/18.
 */

@EApplication
public class WaiMaiApp extends Application{

    @Pref
    public static AppPreferences_ prefs;

    @RestService
    public WaiMaiServer server;

    private User user;

    @AfterInject
    public void afterInject(){
        server.setHeader("Content-Type", "application/json");
        atuoLogin();
    }

    public void atuoLogin(){
        String userName = prefs.userName().get();
        String phone = prefs.phone().get();
        String password = prefs.password().get();
        if(!"".equals(userName) && !"".equals(phone) && !"".equals(password)){
            loginThread(new User(userName, phone, password));
        }
    }

    @Background(id="loginThread")
    public void loginThread(User user){
        try {
            ResponseModel<User> model = server.login(user);
            if(model != null){
                switch (model.getStatus().getStatusCode()) {
                    case ResponseModel.STATUS_LOGIN_SUCCESS:
                        loginSuccess(model.getData());
                        break;
                    case ResponseModel.STATUS_LOGIN_FAILED:
                        loginFailed();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            netError();
        }
    }

    @UiThread
    public void loginSuccess(User user) {
        setUser(user);
        Toast.makeText(getApplicationContext(), "登陆成功，点餐愉快", Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void loginFailed() {
        setUser(null);
        Toast.makeText(getApplicationContext(), "登陆失败，请稍后重试", Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void serverError() {
        Toast.makeText(getApplicationContext(), "服务器异常", Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void netError() {
        Toast.makeText(getApplicationContext(), "网络异常", Toast.LENGTH_SHORT).show();
    }

    public User getUser(){return user;}

    public void setUser(User user){
        this.user = user;
        if(user != null) {
            prefs.userName().put(user.getUserName());
            prefs.phone().put(user.getPhone());
            prefs.password().put(user.getPassword());
        }else{
            prefs.userName().put("");
            prefs.phone().put("");
            prefs.password().put("");
        }
    }
}
